package com.lill.sale.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.util.IdUtil;
import com.lill.product.model.BaseDiscountModel;
import com.lill.product.service.IBaseDiscountService;
import com.lill.sale.dao.ISaleBookDao;
import com.lill.sale.dao.ISaleCarDao;
import com.lill.sale.model.SaleBookModel;
import com.lill.sale.model.SaleCarModel;
import com.lill.sale.service.ISaleCarService;
import com.lill.stock.service.IStockRealService;

/**
 * 销售车辆Service实现类
 *
 * @author lill
 * @date 2021-09-25 11:41:47
 */
@Service("saleCarService")
public class SaleCarServiceImpl implements ISaleCarService {
    @Autowired
    private ISaleCarDao saleCarDao;
    @Autowired
    private ISaleBookDao saleBookDao;
    @Autowired
    private IStockRealService stockRealService;
    @Autowired
    private SaleBookServiceImpl saleBookService;
    @Autowired
    private IBaseDiscountService baseDiscountService;

    public SaleCarModel queryObject(String id) {
    	SaleCarModel saleCar=saleCarDao.queryObject(id);
        return saleCar;
    }

    public List<SaleCarModel> queryList(Map<String, Object> map) {
    	Map<String, Object> mapCol=new HashMap<String, Object>();
    	mapCol.put("companyId", map.get("companyId"));
    	if(map.containsKey("branchId")){
    		mapCol.put("branchId", map.get("branchId"));
    	}else{
    		mapCol.put("branchIdIsNull", map.get("branchIdIsNull"));
    	}
    	List<BaseDiscountModel> baseDiscountList=baseDiscountService.queryList(mapCol);
    	if(CollectionUtils.isNotEmpty(baseDiscountList)){
    		List<String> colNameList=new ArrayList<String>();
    		for (BaseDiscountModel baseDiscount : baseDiscountList) {
    			colNameList.add(baseDiscount.getMapName());
			}
    		map.put("colNameList", colNameList);
    	}
    	List<SaleCarModel> saleCarList=saleCarDao.queryList(map);
//    	if(CollectionUtils.isNotEmpty(saleCarList)){
//    		Map<String, SaleBookModel> mapSaleBook=new HashMap<String, SaleBookModel>();
//    		Map<String, Object> mapCar=new HashMap<String, Object>();
//    		for (SaleCarModel saleCarModel : saleCarList) {
//    			if(mapSaleBook.containsKey(saleCarModel.getSaleBookId())){
//    				saleCarModel.setSaleBook(mapSaleBook.get(saleCarModel.getSaleBookId()));
//    			}else{
//    				SaleBookModel saleBook=saleBookService.queryObjectNoWithSaleCar(saleCarModel.getSaleBookId());
//    				saleCarModel.setSaleBook(saleBook);
//    				mapSaleBook.put(saleCarModel.getSaleBookId(), saleBook);
//    			}
//			}
//    	}
        return saleCarList;
    }

    public int queryTotal(Map<String, Object> map) {
        return saleCarDao.queryTotal(map);
    }

    public int save(SaleCarModel saleCar) {
        saleCar.setId(IdUtil.createIdbyUUID());
        return saleCarDao.save(saleCar);
    }

    public int update(SaleCarModel saleCar) {
        return saleCarDao.update(saleCar);
    }

	public int deleteLogicByMap(Map<String, Object> map) {
		return saleCarDao.deleteLogicByMap(map);
	}

	public int allot(List<SaleCarModel> saleCarList, String userId) {
		int result=0;
		if(CollectionUtils.isNotEmpty(saleCarList)){
			SaleBookModel saleBook=saleBookDao.queryObject(saleCarList.get(0).getSaleBookId());
			if(null!=saleBook){
				for (SaleCarModel saleCar : saleCarList) {
					if(StringUtils.isNotEmpty(saleCar.getVin())){
						Map<String, Object> map=new HashMap<String, Object>();
						map.put("id", saleCar.getId());
						map.put("vin", saleCar.getVin());
						result+=saleCarDao.allot(map);
						stockRealService.updateStore(saleCar.getVin(),saleBook.getPersonId());
					}
				}
				int allotNo=saleCarDao.allotResult(saleCarList.get(0).getSaleBookId());
				if(0==allotNo){
					Map<String, Object> mapParam=new HashMap<String, Object>();
					mapParam.put("updateUser", userId);
					mapParam.put("updateTime", new Date());
					mapParam.put("id", saleCarList.get(0).getSaleBookId());
					mapParam.put("state", 3);
					mapParam.put("newState", 4);
					saleBookDao.updateSate(mapParam);
				}
			}
		}
		return result;
	}
}
